package com.wanhelibs.custom_view.views;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ListView;
import android.widget.TextView;

import com.wanhe.base.BaseDialogFragment;
import com.wanhe.base.model.base.Dish;
import com.wanhe.base.model.base.SetMealDish;
import com.wanhe.shoppingcart.bean.CartItem;
import com.wanhe.shoppingcart.bean.SetMealCartItem;
import com.wanhe.shoppingcart.manager.CartManager;
import com.wanhelibs.R;
import com.wanhelibs.adapter.AddSetMealAdapter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by XieZaiWei on 2017/8/10.
 * 加套餐Dialog
 */

public class AddSetMealDialog extends BaseDialogFragment implements View.OnClickListener {
	private TextView	tv_title;
	private ListView	lv_setMeal;
	private TextView	tv_cancle;
	private TextView	tv_ok;

	private Dish				mDish;
	private AddSetMealAdapter	mAddSetMealAdapter;

	public static AddSetMealDialog newInstance() {
		AddSetMealDialog dialog = new AddSetMealDialog();
		return dialog;
	}

	public void setDish(Dish dish) {
		mDish = dish;
	}

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
		getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
		mView = inflater.inflate(R.layout.add_set_meal_dialog, container, false);
		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		tv_title = (TextView) findViewById(R.id.tv_title);
		lv_setMeal = (ListView) findViewById(R.id.lv_setMeal);
		tv_cancle = (TextView) findViewById(R.id.tv_cancle);
		tv_ok = (TextView) findViewById(R.id.tv_ok);

		tv_title.setText(mDish.getVch_dishname());
		tv_cancle.setOnClickListener(this);
		tv_ok.setOnClickListener(this);
		init();
	}

	@Override
	public void onStart() {
		super.onStart();
		Dialog dialog = getDialog();
		if (dialog != null) {
			DisplayMetrics dm = new DisplayMetrics();
			getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
			dialog.getWindow().setLayout((int) (dm.widthPixels * 0.4), (int) (dm.widthPixels * 0.5));
		}
	}

	private void init() {
		Collection <SetMealDish> valueCollection = mDish.transformToSetMealDish().values();
		List <SetMealDish> arrSetMealDish = new ArrayList<>(valueCollection);
		mAddSetMealAdapter = new AddSetMealAdapter(getContext(), arrSetMealDish);
		lv_setMeal.setAdapter(mAddSetMealAdapter);
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.tv_ok :
				SetMealCartItem setMealCartItem = new SetMealCartItem();
				for (int i = 0; i < mAddSetMealAdapter.getCount(); i++) {
					Dish dish = mAddSetMealAdapter.getItem(i).currentDish;
					CartItem cartItem = new CartItem();
					cartItem.setDish(dish);
					cartItem.setCount(1);
					setMealCartItem.addChildItem(cartItem);
				}
				setMealCartItem.setDish(mDish);
				CartManager.getInstance().addItem(setMealCartItem, 1);
				dismiss();
				break;

			case R.id.tv_cancle :
				dismiss();
				break;
			default :
				break;
		}
	}
}
